<template>
  <div class="add">
    <BaseDialog
      width="1200px"
      :title="isType === 'add' ? '新增' : '编辑'"
      v-model:visible="innerVisible"
      :renderDefaultFooter="false"
    >
      <MyForm
        v-model:formData="formData"
        v-model:columns="formColumns"
        :rules="rules"
        :labelWidth="120"
        :saveBtnLoading="saveBtnLoading"
        background="#fff"
        @submit="onSubmit"
        @cancel="innerVisible = false"
      >
      </MyForm>
    </BaseDialog>
  </div>
</template>
<script setup>
import { ref, computed, onMounted, watch } from "vue";
import * as api from "@/api/index.js";
import i18n from "@/lang/index.js";
import { getSubZhEn, columnFilters } from "@/utils/mUtils.js";
import { ElMessage } from "element-plus";
const props = defineProps({
  visible: {
    type: Boolean,
  },
  currRow: {
    type: Object,
    default: () => null,
  },
  isType: {
    type: String,
    default: "add",
  },
});
const emits = defineEmits(["update:visible", "onSubmit"]);
const innerVisible = computed({
  get() {
    return props.visible;
  },
  set(val) {
    emits("update:visible", val);
  },
});
const formData = ref({
  enableFlag: 1,
});
const formColumns = ref([
  {
    title: "",
    structure: [
      {
        content: [
          {
            x: 0,
            y: 1,
            w: 8,
            h: 1,
            i: "1",
            prop: "ruleName",
            label: "规则名",
            type: "label_input",
          },
          {
            x: 8,
            y: 1,
            w: 5,
            h: 1,
            i: "2",
            prop: "calcBillType",
            label: "计费方式",
            type: "label_select",
            options: [
              {
                label: "KGS计重",
                value: 0,
              },
              {
                label: "CBM计方",
                value: 1,
              },
            ],
          },
          {
            x: 13,
            y: 1,
            w: 3,
            h: 1,
            i: "3",
            prop: "calcType",
            labelWidth: "0px",
            type: "label_select",
            options: [
              {
                label: "按票",
                value: 0,
              },
              {
                label: "按单件",
                value: 1,
              },
            ],
          },
          {
            x: 16,
            y: 1,
            w: 8,
            h: 1,
            i: "4",
            label: "转换系数",
            prop: "conversionFactor",
            type: "label_number",
            content: "材积系数：5000、6000；重货系数：363、500",
          },
          {
            x: 0,
            y: 2,
            w: 8,
            h: 1,
            i: "5",
            label: "单件低消值",
            prop: "pcsMinCost",
            type: "label_number",
            content: "每一件的最低费用值",
          },
          {
            x: 8,
            y: 2,
            w: 8,
            h: 1,
            i: "6",
            label: "整票低消值",
            prop: "ticketMinCost",
            type: "label_number",
            content: "整票的最低费用值",
          },
          {
            x: 16,
            y: 1,
            w: 8,
            h: 1,
            i: "7",
            label: "单件KG重量",
            max: 10,
            prop: "pcsWeightScale",
            selectProp: "pcsWeightCarry",
            type: "label_input_select",
            props: {
              label: "label",
              value: "value",
            },
            options: [
              { value: 0, label: "向上进位" },
              { value: 1, label: "四舍五入" },
              { value: 2, label: "以5进位" },
            ],
            content: "KG常用2位；CBM常用3位",
          },
          {
            x: 0,
            y: 3,
            w: 8,
            h: 1,
            i: "8",
            max: 10,
            label: "整票KG重量",
            prop: "ticketWeightScale",
            selectProp: "ticketWeightCarry",
            type: "label_input_select",
            props: {
              label: "label",
              value: "value",
            },
            options: [
              { value: 0, label: "向上进位" },
              { value: 1, label: "四舍五入" },
              { value: 2, label: "以5进位" },
            ],
            content: "KG常用2位；CBM常用3位",
          },
          {
            x: 8,
            y: 3,
            w: 8,
            h: 1,
            i: "9",
            label: "单件体积小数位",
            prop: "pcsVolumeScale",
            max: 10,
            selectProp: "pcsVolumeCarry",
            type: "label_input_select",
            props: {
              label: "label",
              value: "value",
            },
            options: [
              { value: 0, label: "向上进位" },
              { value: 1, label: "四舍五入" },
              { value: 2, label: "以5进位" },
            ],
            content: "由长宽高计算的体积值需保留的小数位,位数越大,越精准",
          },
          {
            x: 16,
            y: 3,
            w: 8,
            h: 1,
            i: "10",
            label: "整票体积小数位",
            max: 10,
            prop: "ticketVolumeScale",
            selectProp: "ticketVolumeCarry",
            type: "label_input_select",
            props: {
              label: "label",
              value: "value",
            },
            options: [
              { value: 0, label: "向上进位" },
              { value: 1, label: "四舍五入" },
              { value: 2, label: "以5进位" },
            ],
            content: "由长宽高计算的体积值需保留的小数位,位数越大,越精准",
          },
          {
            x: 0,
            y: 4,
            w: 8,
            h: 1,
            i: "11",
            label: "起底件数",
            prop: "minPcs",
            type: "label_number",
            content: "整票最少几件货起送",
          },
          {
            x: 8,
            y: 4,
            w: 8,
            h: 1,
            i: "12",
            label: "分泡比例",
            prop: "bubbleRatio",
            type: "label_select",
            props: {
              value: "subValue",
              label: "subName",
            },
            options: [],
            content:
              "分泡比例,比例值为分泡下来的取值。如70%泡，则是按泡量的70%取值。",
          },
          {
            x: 0,
            y: 5,
            w: 24,
            h: 2,
            i: "13",
            label: "适用范围/备注",
            prop: "remark",
            inputType: "textarea",
            type: "label_input",
          },
          {
            x: 0,
            y: 5,
            w: 24,
            h: 2,
            i: "14",
            label: "声明条款",
            prop: "declarationClause",
            inputType: "textarea",
            type: "label_input",
          },
        ],
      },
    ],
  },
]);
const rules = ref({});
const saveBtnLoading = ref(false);
onMounted(() => {
  onGetSub();
  if (props.isType === "edit") {
    getInfo();
    return;
  }
});
const onGetSub = () => {
  getSubZhEn({
    code: "029",
  }).then((res) => {
    columnFilters(formColumns.value, "bubbleRatio").options = res["029"];
  });
};
const getInfo = async () => {
  saveBtnLoading.value = true;
  try {
    const { data } = await api.default.common.weighing.getInfo(
      props.currRow.id
    );
    formData.value = {
      ...data,
    };
    saveBtnLoading.value = false;
  } catch (err) {
    saveBtnLoading.value = false;
  }
};

const onSubmit = () => {
  const parmas = {
    ...formData.value,
  };
  if (props.isType === "add") {
    // 新增
    saveBtnLoading.value = true;
    api.default.common.weighing
      .add({
        ...parmas,
      })
      .then(() => {
        ElMessage.success("新增成功");
        innerVisible.value = false;
        saveBtnLoading.value = false;
        emits("onSubmit");
      })
      .catch(() => {
        saveBtnLoading.value = false;
      });
  } else {
    // 编辑
    saveBtnLoading.value = true;
    api.default.common.weighing
      .edit(parmas)
      .then(() => {
        ElMessage.success("修改成功");
        innerVisible.value = false;
        saveBtnLoading.value = false;
        emits("onSubmit");
      })
      .catch(() => {
        saveBtnLoading.value = false;
      });
  }
};
</script>
<style lang="scss" scoped>
.search-btn {
  width: 120px;
  margin: 20px auto 0;
}
</style>
